package TUILayer;

import ControlLayer.PersonCtr;
import ModelLayer.RealException;

public class LoginInfo {

	private InputParser inputParser;
	private PersonCtr personCtr;
	private int loggedEmployeeId = 0;

	public LoginInfo(InputParser inputParser) {
		this.inputParser = inputParser;
		personCtr = new PersonCtr();
	}

	public void start() throws RealException {
		while (loggedEmployeeId == 0) {
			if (personCtr.getTotalEmployees() == 0) {
				System.out
						.println("\f ***Login ***\n\n No employees found. Employee \"Admin\" with password \"admin\" was created.");
				personCtr.createEmployee("Admin", "N/A", 0, "admin", "admin");
			} else {
				String name = inputParser
						.getString("\f ***Login ***\n\nPlease input your name: ");
				String password = inputParser
						.getString("\f ***Login ***\n\nPlease input your password: ");
				try {
					loggedEmployeeId = personCtr.login(name, password);
					System.out
							.println("\f ***Login ***\n\n You are logged in as "
									+ name);
				} catch (RealException e) {
					System.out.println("\f ***Login ***\n\n" + e.getError());
				}
			}
			inputParser.printReturn();
		}
	}

	public int getLoggedEmployeeId() {
		return this.loggedEmployeeId;
	}

	public int getLoggedEmployeePositionId() throws RealException {
		if (loggedEmployeeId != 0) {
			String position = personCtr.getEmployeePosition(loggedEmployeeId);
			if(position.equals("manager")) return 2;
			else if (position.equals("salesman")) return 3;
			else if (position.equals("admin")) return 1;
		}
		return 0;
	}
}
